#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <math.h>

#define INT32 int
#define IPPING_RESULT_FILE "./result_ipping.txt"

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

typedef struct _ipping{
    char state[64];
    char interface[256];
    char host[256];
    char repetitions[32];
    char timeout[32];
    char data_size[32];
    char dscp[32];
    char succ_cnt[32];
    char fail_cnt[32];
    char avg_res_time[32];
    char min_res_time[32];
    char max_res_time[32];
    char avg_res_time_dtl[32];
    char min_res_time_dtl[32];
    char max_res_time_dtl[32];
}IPPING_T;

static IPPING_T ipping={"None", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};

void ipping_start()
{
	char cmd[128] = {0};

	//sprintf(cmd, "ping -c 5 -s 1024 -I enp0s8 -W 10 192.168.0.101 > %s", IPPING_RESULT_FILE);
	sprintf(cmd, "ping -c 2 -s 1024 -W 10 192.168.0.101 > %s", IPPING_RESULT_FILE);
	system(cmd);
}

INT32 ipping_load_result(char *file)
{
    FILE *fp = NULL;
    //char buff[512] = {0};
    //char buff[512] = "rtt min/avg/max/mdev = 0.411/0.453/0.495/0.042 ms";
    char buff[512] = "round-trip min/avg/max = 2.573/3.158/5.247 ms";
	char pkt_send[32] = {0};
	char pkt_recv[32] = {0};
	char rate_loss[32] = {0};
	char min[32] = {0};
	char avg[32] = {0};
	char max[32] = {0};
	double dmin;
	double davg;
	double dmax;
	int ret = 0;

    fp = fopen(file, "r");
    if(fp == NULL)
        return 0;

    printf("<----- file = [%s] ----->\n", file);
    //while(fgets(buff, sizeof(buff), fp)){
        //if(3 == sscanf(buff, "%[0-9] packets transmitted, %[0-9] packets received, %s packet loss*", 
#if 0
        if(2 == sscanf(buff, "%[0-9] packets transmitted, %[0-9] *", pkt_send, pkt_recv)){
			printf("dbg---> send=[%s], recv=[%s], loss=[%s]\n", pkt_send, pkt_recv, rate_loss);
			sprintf(ipping.succ_cnt, "%s", pkt_recv);
			sprintf(ipping.fail_cnt, "%d", atoi(pkt_send) - atoi(pkt_recv));
		}

        if(0 < sscanf(buff, "*min*avg*max* = %s", min)){
			printf("dbg---> min=[%s], avg=[%s], max=[%s]\n", min, avg, max);
		}
#endif

        //if(0 < sscanf(buff, "* min/avg/max* = %*[^/]/%*[^/]/%*[^/]*", min, avg, max)){	
		printf("buff=[%s]\n", buff);
        //if(0 < sscanf(buff, "*= %[0-9.]/%[0-9.]/%[0-9.]*", min, avg, max)){
        ret =  sscanf(buff, "%*[^=]= %[^/]/%[^/]/%[^/] *", min, avg, max);
		printf("ret = [%d]\n", ret);
		if(ret > 0){
			printf("dbg---> a min=[%s], avg=[%s], max=[%s]\n", min, avg, max);
			dmin = atof(min);
			davg = atof(avg);
			dmax = atof(max);
			printf("dbg---> a dmin=[%.3f], davg=[%.3f], dmax=[%.3f]\n", dmin, davg, dmax);
			printf("dbg---> a dmin=[%d], davg=[%d], dmax=[%d]\n", (int)round(dmin), (int)round(davg), (int)round(dmax));
		}
    //}
    fclose(fp);
	
	return 0;
}

void ipping_print()
{
}


int main(int argc, char *argv[])
{
	int index;

	//ipping_start();
	ipping_load_result(IPPING_RESULT_FILE);
	ipping_print();

    return 0;
}
